File: eml_setop.c

    1   /*
    2    * File: eml_setop.c
    3    *
    4    * MATLAB Coder version            : 3.1
    5    * C/C++ source code generated on  : 22-Apr-2021 15:09:40
    6    */
    7   
    8   /* Include Files */
    9   #include "rt_nonfinite.h"
   10   #include "matlabfindpeaks.h"
   11   #include "eml_setop.h"
   12   #include "matlabfindpeaks_emxutil.h"
   13   
   14   /* Function Declarations */
   15   static double skip_to_last_equal_value(int *k, const emxArray_real_T *x);
   16   
   17   /* Function Definitions */
   18   
   19   /*
   20    * Arguments    : int *k
   21    *                const emxArray_real_T *x
   22    * Return Type  : double
   23    */
   24   static double skip_to_last_equal_value(int *k, const emxArray_real_T *x)
   25   {
   26     double xk;
   27     boolean_T exitg1;
   28     double absxk;
   29     int exponent;
   30     boolean_T p;
   31     xk = x->data[*k - 1];
   32     exitg1 = false;
   33     while ((!exitg1) && (*k < x->size[0])) {
   34       absxk = fabs(xk / 2.0);
   35       if (absxk <= 2.2250738585072014E-308) {
   36         absxk = 4.94065645841247E-324;
   37       } else {
   38         frexp(absxk, &exponent);
   39         absxk = ldexp(1.0, exponent - 53);
   40       }
   41   
   42       p = (fabs(xk - x->data[*k]) < absxk);
   43       if (p) {
   44         (*k)++;
   45       } else {
   46         exitg1 = true;
   47       }
   48     }
   49   
   50     return xk;
   51   }
   52   
   53   /*
   54    * Arguments    : const emxArray_real_T *a
   55    *                const emxArray_real_T *b
   56    *                emxArray_real_T *c
   57    *                emxArray_int32_T *ia
   58    *                emxArray_int32_T *ib
   59    * Return Type  : void
   60    */
   61   void do_vectors(const emxArray_real_T *a, const emxArray_real_T *b,
   62                   emxArray_real_T *c, emxArray_int32_T *ia, emxArray_int32_T *ib)
   63   {
   64     int na;
   65     int nb;
   66     int ncmax;
   67     int ibfirst;
   68     int nc;
   69     int nia;
   70     int nib;
   71     int iafirst;
   72     int ialast;
   73     int iblast;
   74     int b_ialast;
   75     double ak;
   76     int b_iblast;
   77     double bk;
   78     double absxk;
   79     emxArray_int32_T *b_ia;
   80     int exponent;
   81     emxArray_int32_T *b_ib;
   82     emxArray_real_T *b_c;
   83     na = a->size[0];
   84     nb = b->size[0];
   85     ncmax = a->size[0] + b->size[0];
   86     ibfirst = c->size[0];
   87     c->size[0] = ncmax;
   88     emxEnsureCapacity((emxArray__common *)c, ibfirst, (int)sizeof(double));
   89     ibfirst = ia->size[0];
   90     ia->size[0] = a->size[0];
   91     emxEnsureCapacity((emxArray__common *)ia, ibfirst, (int)sizeof(int));
   92     ibfirst = ib->size[0];
   93     ib->size[0] = b->size[0];
   94     emxEnsureCapacity((emxArray__common *)ib, ibfirst, (int)sizeof(int));
   95     nc = -1;
   96     nia = -1;
   97     nib = 0;
   98     iafirst = 1;
   99     ialast = 1;
  100     ibfirst = 0;
  101     iblast = 1;
  102     while ((ialast <= na) && (iblast <= nb)) {
  103       b_ialast = ialast;
  104       ak = skip_to_last_equal_value(&b_ialast, a);
  105       ialast = b_ialast;
  106       b_iblast = iblast;
  107       bk = skip_to_last_equal_value(&b_iblast, b);
  108       iblast = b_iblast;
  109       absxk = fabs(bk / 2.0);
  110       if (absxk <= 2.2250738585072014E-308) {
  111         absxk = 4.94065645841247E-324;
  112       } else {
  113         frexp(absxk, &exponent);
  114         absxk = ldexp(1.0, exponent - 53);
  115       }
  116   
  117       if (fabs(bk - ak) < absxk) {
  118         nc++;
  119         c->data[nc] = ak;
  120         nia++;
  121         ia->data[nia] = iafirst;
  122         ialast = b_ialast + 1;
  123         iafirst = b_ialast + 1;
  124         iblast = b_iblast + 1;
  125         ibfirst = b_iblast;
  126       } else if (ak < bk) {
  127         nc++;
  128         nia++;
  129         c->data[nc] = ak;
  130         ia->data[nia] = iafirst;
  131         ialast = b_ialast + 1;
  132         iafirst = b_ialast + 1;
  133       } else {
  134         nc++;
  135         nib++;
  136         c->data[nc] = bk;
  137         ib->data[nib - 1] = ibfirst + 1;
  138         iblast = b_iblast + 1;
  139         ibfirst = b_iblast;
  140       }
  141     }
  142   
  143     while (ialast <= na) {
  144       iafirst = ialast;
  145       ak = skip_to_last_equal_value(&iafirst, a);
  146       nc++;
  147       nia++;
  148       c->data[nc] = ak;
  149       ia->data[nia] = ialast;
  150       ialast = iafirst + 1;
  151     }
  152   
  153     while (iblast <= nb) {
  154       iafirst = iblast;
  155       bk = skip_to_last_equal_value(&iafirst, b);
  156       nc++;
  157       nib++;
  158       c->data[nc] = bk;
  159       ib->data[nib - 1] = iblast;
  160       iblast = iafirst + 1;
  161     }
  162   
  163     if (a->size[0] > 0) {
  164       if (1 > nia + 1) {
  165         iafirst = -1;
  166       } else {
  167         iafirst = nia;
  168       }
  169   
  170       emxInit_int32_T(&b_ia, 1);
  171       ibfirst = b_ia->size[0];
  172       b_ia->size[0] = iafirst + 1;
  173       emxEnsureCapacity((emxArray__common *)b_ia, ibfirst, (int)sizeof(int));
  174       for (ibfirst = 0; ibfirst <= iafirst; ibfirst++) {
  175         b_ia->data[ibfirst] = ia->data[ibfirst];
  176       }
  177   
  178       ibfirst = ia->size[0];
  179       ia->size[0] = b_ia->size[0];
  180       emxEnsureCapacity((emxArray__common *)ia, ibfirst, (int)sizeof(int));
  181       iafirst = b_ia->size[0];
  182       for (ibfirst = 0; ibfirst < iafirst; ibfirst++) {
  183         ia->data[ibfirst] = b_ia->data[ibfirst];
  184       }
  185   
  186       emxFree_int32_T(&b_ia);
  187     }
  188   
  189     if (b->size[0] > 0) {
  190       if (1 > nib) {
  191         iafirst = 0;
  192       } else {
  193         iafirst = nib;
  194       }
  195   
  196       emxInit_int32_T(&b_ib, 1);
  197       ibfirst = b_ib->size[0];
  198       b_ib->size[0] = iafirst;
  199       emxEnsureCapacity((emxArray__common *)b_ib, ibfirst, (int)sizeof(int));
  200       for (ibfirst = 0; ibfirst < iafirst; ibfirst++) {
  201         b_ib->data[ibfirst] = ib->data[ibfirst];
  202       }
  203   
  204       ibfirst = ib->size[0];
  205       ib->size[0] = b_ib->size[0];
  206       emxEnsureCapacity((emxArray__common *)ib, ibfirst, (int)sizeof(int));
  207       iafirst = b_ib->size[0];
  208       for (ibfirst = 0; ibfirst < iafirst; ibfirst++) {
  209         ib->data[ibfirst] = b_ib->data[ibfirst];
  210       }
  211   
  212       emxFree_int32_T(&b_ib);
  213     }
  214   
  215     if (ncmax > 0) {
  216       if (1 > nc + 1) {
  217         iafirst = -1;
  218       } else {
  219         iafirst = nc;
  220       }
  221   
  222       emxInit_real_T(&b_c, 1);
  223       ibfirst = b_c->size[0];
  224       b_c->size[0] = iafirst + 1;
  225       emxEnsureCapacity((emxArray__common *)b_c, ibfirst, (int)sizeof(double));
  226       for (ibfirst = 0; ibfirst <= iafirst; ibfirst++) {
  227         b_c->data[ibfirst] = c->data[ibfirst];
  228       }
  229   
  230       ibfirst = c->size[0];
  231       c->size[0] = b_c->size[0];
  232       emxEnsureCapacity((emxArray__common *)c, ibfirst, (int)sizeof(double));
  233       iafirst = b_c->size[0];
  234       for (ibfirst = 0; ibfirst < iafirst; ibfirst++) {
  235         c->data[ibfirst] = b_c->data[ibfirst];
  236       }
  237   
  238       emxFree_real_T(&b_c);
  239     }
  240   }
  241   
  242   /*
  243    * File trailer for eml_setop.c
  244    *
  245    * [EOF]
  246    */
  247